<script type="text/javascript">
			var codeCreater_list = null;
			var i = 0,
            getRow = function () {
	                return {id:++i,type:"String",name:"name",tableFieldName:"NAME",formField:"true",showName:"名称",required:"false"};
	            },
            getIdRow = function () {
                return {id:++i,type:"Long",name:"id",tableFieldName:"ID",formField:"false",showName:"ID编号",required:"false"};
            };
			$(function() {
				codeCreater_list = $('#codeCreater_list');
				codeCreater_list.datagrid({
					url : 'codeCreater.do?loadModelFields',
					height : 'auto',
					striped : 'false',
					fit : true,
					remoteSort: false,
					toolbar : '#codeCreaterlist_search',
					loadMsg : '数据加载中，请稍等...',
					queryParams : {
						time : new Date().getTime(),
						tableId:0
					},
					rownumbers : true,
					singleSelect : true,
					idField : 'id',
	                rowContextMenu: [
	                                 { text: "编辑", iconCls: "icon-edit", handler: function (e, index) { codeCreater_list.datagrid("beginEdit", index); } }
	                             ],
					columns : [ [ {
						field : 'id',
						checkbox : true
					}, {
						field : 'type',
						width : 80,
						title : '属性类型',
						editor : {
							type:'combobox',
							options:{
								valueField:'value',
								textField:'name',
								url:'common.do?loadComboBox&key=FIELD_TYPE',
								required:true
							}
						},
					}, {
						field : 'name',
						width : 80,
						title : '属性名称',
						editor : {
							type : 'text'
						},
					}, {
						field : 'tableFieldName',
						width : 150,
						title : '属性对应表字段名称',
						editor : {
							type : 'text'
						},
					}, {
						field : 'formField',
						width : 80,
						title : '是否表单元素',
						editor : {
							type:'combobox',
							options:{
								valueField:'value',
								textField:'name',
								url:'common.do?loadComboBox&key=YON',
								required:true
							}
						},
					}, {
						field : 'showName',
						width : 100,
						title : '显示名称',
						editor : {
							type : 'text'
						},
					}, {
						field : 'required',
						width : 80,
						title : '是否必填',
						editor : {
							type:'combobox',
							options:{
								valueField:'value',
								textField:'name',
								url:'common.do?loadComboBox&key=YON',
								required:true
							}
						},
					}, ] ],
					checkOnSelect:true,
					enableHeaderClickMenu: false,
	                enableHeaderContextMenu: false,
					enableRowContextMenu: false,
					autoEditing: true,          //该属性启用双击行时自定开启该行的编辑状态
		            extEditing: true,           //该属性启用行编辑状态的 ExtEditing 风格效果，该属性默认为 true。
		            singleEditing: true//该属性为 false 时禁用 datagrid 的只允许单行编辑效果，也就是允许多行同时编辑
				});
// 				codeCreater_list.datagrid("appendRow", getRow());
			});
			
			
	        function addModelField() {
	        	codeCreater_list.datagrid("appendRow", getRow());
	        }
	        
	        function removeModelField() {
	        	var selectRecord = codeCreater_list.datagrid("getSelected");
	        	var rowIndex = codeCreater_list.datagrid('getRowIndex', selectRecord);
	        	printLog(selectRecord);
	        	codeCreater_list.datagrid('deleteRow', rowIndex);
	        }
	        
	        //下载代码文件
			function downLoadCodeAction(folderName) {
	   			if($('#downloadForm')) $('#downloadForm').remove();
	   			var downloadForm  = $("<form id=\"downloadForm\">");//定义一个form表单
	   			downloadForm.attr("style","display:none");
	   			downloadForm.attr("target","");
	   			downloadForm.attr("method","post");
	   			downloadForm.attr("action",'codeCreater.do?downLoadCode&folderName='+folderName);
	   			var input1=$("<input>");
	   			input1.attr("type","hidden");
	   			input1.attr("name","downLoadCode");
	   			input1.attr("value",(new Date()).getMilliseconds()); 
	   			$("body").append(downloadForm);//将表单放置在web中
	   			downloadForm.append(input1);
	   			downloadForm.submit();//表单提交
	   			return false;
			}
	        
	        function createCode(needDownLoad) {
	        	var inserted = codeCreater_list.datagrid('getRows');//codeCreater_list.datagrid('getChanges', "inserted");
	        	printLog(inserted);
	        	var coder = [{
	        		model:{
	        		id:$('#modelTableId').val(),
	        		moduleName:$('#moduleName').val(),
        			modelName:$('#modelName').val(),
        			tableName:$('#tableName').val(),
        			comment:$('#comment').val()},
        			fields:inserted
	        	}];
	        	$.post("codeCreater.do?createCode", {data:JSON.stringify(coder)}, function(result) {
	                   if(result.success){
// 	                   	   $("#buttonlist").datagrid('acceptChanges');
	                   	   reloadModelTable();
	                   	   if(needDownLoad) {
		                   		$.messager.confirm('询问','下载代码文件吗?',function(r){
			    		        	if (r){
			    		        		downLoadCodeAction(result.obj);
			    		            }
			    		        });
	                   	   }else {
	                   		 showMsg(result.msg);
	                   	   }
	                   }else {
	                	   showMsg(result.msg);
	                   }
	           }, "JSON");
	        }
	        
	        function addModelTable() {
	        	$('#codeCreaterForm').form('clear');
	        	var coder = {
	        			id:'',
	        			moduleName:'shop',
	        			modelName:'goods',
	        			tableName:'SHOP_GOODS',
	        			comment:'说明'
	    			};
    			$("#codeCreaterForm").form('load', coder);
//     			refreshModelFields(0);
				codeCreater_list.datagrid('loadData', { total: 0, rows: [] });//清空下方属性表格 
    			codeCreater_list.datagrid("appendRow", getIdRow());
	        }
	        function deleteModelTable() {
	        	var rows = modelTable_list.datagrid('getChecked');
	    	    if (rows&&rows.length>0){
	    	        $.messager.confirm('询问','你确认要删除记录吗?',function(r){
	    	            if (r){
	    	            	var ids = "";
	    	            	for(var i=0; i<rows.length; i++){
	    	            		ids += rows[i].id+",";
	    	            	}
	    	                $.post('codeCreater.do?deleteCode',{ids:ids},function(result){
	    	                    if (result.success){
	    	                    	reloadModelTable();
	    	                    }
	    	                    showMsg(result.msg);
	    	                },'json');
	    	            }
	    	        });
	    	    }else {
	    	    	showMsg("请选择操作记录.");
	    	    }
	        }
	        function reloadModelTable() {
	        	$('#codeCreaterForm').form('clear');
	        	modelTable_list.datagrid('reload'); //设置好查询参数 reload 一下就可以了  
	        	modelTable_list.datagrid('unselectAll');
	        	modelTable_list.datagrid('uncheckAll');
	        	refreshModelFields(0);
	        }
	        
	        function refreshModelFields(tableId) {
	        	//先取得 datagrid 的查询参数  
	    		var params = codeCreater_list.datagrid('options').queryParams;
	    		params['tableId'] = tableId;
	    		//设置好查询参数 reload 一下就可以了 
	    		codeCreater_list.datagrid('reload');  
	        }
	        
	        function onClickTableModel(rowIndex, rowData) {
// 	        	printLog(rowData);
	        	$("#codeCreaterForm").form('load', rowData);
// 	        	$.post('codeCreater.do?loadModelFields',{tableId:rowData.id},function(data){
// 	        		printLog(data);
// 	        		codeCreater_list.datagrid('unselectAll');
// 	        		codeCreater_list.datagrid('uncheckAll');
//                     codeCreater_list.datagrid('loadData',data);
//                 },'json');
				refreshModelFields(rowData.id);
	        	
	        }
		</script>
<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'west',split:true,border:false" style="width:400px;">
		<@eu.datagrid id="modelTable_list" idField="id" title="历史生成记录"  pagination="false" toolbar="#modelTable_toolbar"
		url="codeCreater.do?loadModels" item="com.xdtech.coder.vo.ModelTableItem"
		onClickRow="onClickTableModel(rowIndex, rowData)" />
		<div id="modelTable_toolbar" style="height: auto">
			<@eu.linkbutton showName="新增" iconCls="icon-add" onclick="addModelTable()"/>
			<@eu.linkbutton showName="删除" iconCls="icon-remove" onclick="deleteModelTable()"/>
			<@eu.linkbutton showName="刷新" iconCls="icon-reload" onclick="reloadModelTable()"/>
		</div>
	</div>
	<div data-options="region:'center',border:false">
		<table id="codeCreater_list" data-options="border:false"></table>
		<div id="codeCreaterlist_search">
		   <form id="codeCreaterForm" action="codeCreater.do?editCoder">
				<table>
		    		<tr>
		    			<input id="modelTableId" type="hidden" name="id"/>
		    			<td>模块名称(英文):</td>
		    			<td>
		    				<input id="moduleName" name="moduleName" class="easyui-validatebox" data-options="required: true, width: 150, prompt: 'sys'" value="" />
		    			</td>
		    			<td>模型名称(英文):</td>
		    			<td>
		    				<input id="modelName" name="modelName" class="easyui-validatebox" data-options="required: true, width: 150, prompt: 'user'" value="" />
		    			</td>
		    			<td>表名:</td>
		    			<td>
		    				<input id="tableName" name="tableName" class="easyui-validatebox" data-options="required: true, width: 150, prompt: 'SYS_USER'" value="" />
		    			</td>
		    			<td>说明:</td>
		    			<td>
		    				<input id="comment" name="comment" class="easyui-validatebox" data-options="required: true, width: 150, prompt: '说明'" value="" />
		    			</td>
		    		</tr>
		    	</table>
			</form>
			<div>
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addModelField()">新增属性</a>
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeModelField()">移除属性</a>
			</div>
		</div>
		
	</div>
	<div data-options="region:'south',border:false">
		<div style="padding:3px 0;border-top:1px solid #95B8E7;text-align: center;">
			<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" style="width:100px;" onclick="createCode(false)">确定保存</a>
	        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" style="width:100px;" onclick="createCode(true)">确定并下载</a>
	    </div>
	</div>
</div>